package com.medical.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.config.AlipayConfig;
import com.medical.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年11月27日 下午9:19:29 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="Main/Test")
public class TestController {
	@RequestMapping(value="/alipayTest")
	public ModelAndView alipayTest()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/test/index");
		return model;
	}
	@RequestMapping(value="testOrder")
	public void testOrder(HttpServletRequest req,Model mod,HttpServletResponse rep)throws Exception{
		HttpSession session=req.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String Param="hosploginid="+hosploginid+"&hosporderid=27";
		System.out.println("输出param是："+Param);
		//获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = "20171234567";
        //付款金额，必填
        String total_amount = "200";
        //订单名称，必填
        String subject = "测试订单";
        //商品描述，可空
        String body = "商品描述";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
                + "\"total_amount\":\""+ total_amount +"\"," 
                + "\"subject\":\""+ subject +"\"," 
                + "\"body\":\""+ body +"\"," 
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result1 = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("输出result1："+result1);
        JSONObject json = HttpRequest.sendPost("paydoctor",Param);
		System.out.println("医院确认押金增加返回json："+json);
        Integer code = Integer.parseInt(json.get("code").toString());
        String msg = json.get("msg").toString();
        rep.setContentType("text/html;charset=" + AlipayConfig.charset);
        if(code==200) {
        	PrintWriter out = rep.getWriter();
        	out.print("<h4>");
        	out.println(msg);
            out.print("</h4>");
        	out.println("<script language='javascript'>");   
            out.println("alert('"+msg+"');");
            out.print("</script>");
        }else {
        	String result = json.get("data").toString();
        	rep.getWriter().write(result);//直接将完整的表单html输出到页面
            rep.getWriter().flush();
            rep.getWriter().close();
        }
	}
	/**
	 * 支付宝交易关闭，测试中，，，
	 * @param req
	 * @param mod
	 * @param rep
	 * @throws Exception
	 */
	@RequestMapping(value="/testclose")
	public void testclose(HttpServletRequest req,Model mod,HttpServletResponse rep)throws Exception{
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
		
		//设置请求参数
		AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
		//商户订单号，商户网站订单系统中唯一订单号
		String out_trade_no = "h27d20171128151355552000";
		//支付宝交易号
		String trade_no = "2017112821001004880200395074";
		//请二选一设置
		
		alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," +"\"trade_no\":\""+ trade_no +"\"}");
		
		//请求
		String result = alipayClient.execute(alipayRequest).getBody();
		System.out.println("输出result："+result);
		rep.setContentType("text/html;charset=" + AlipayConfig.charset);
		rep.getWriter().write(result);//直接将完整的表单html输出到页面
	    rep.getWriter().flush();
	    rep.getWriter().close();
	}
	
	@RequestMapping(value="/testlogin")
	public ModelAndView testlogin()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/test/login");
		return model;
	}
	@RequestMapping(value="/testloginsubmit")
	public Map<String,Object> testloginsubmit()throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		return map;
	}
	@RequestMapping(value="/websocket")
	public ModelAndView websocket()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/test/websocket");
		return model;
	}
	@RequestMapping(value="/testJiaMi")
	public ModelAndView testJiaMi()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/test/testJiaMi");
		return model;
	}
}
